

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Han Qiu">
  <meta name="keywords" content="">
  
    <meta name="description" content="一、v-text 与 Mustache比较相似：都用于将数据显示在界面中（没有Mustache灵活）   二、v-html 会将string的html解析出来并进行渲染   三、v-show 用法与v-if相似，也用于决定一个元素是否渲染  1、v-if和v-show对比 v-if：当条件为false时，包含v-if指令的元素，根本就不会存在于dom中 v-show：当条件为false时，v-sh">
<meta property="og:type" content="article">
<meta property="og:title" content="vue常用指令">
<meta property="og:url" content="http://example.com/2022/01/06/vue%E5%B8%B8%E7%94%A8%E6%8C%87%E4%BB%A4/index.html">
<meta property="og:site_name" content="寒秋の博客">
<meta property="og:description" content="一、v-text 与 Mustache比较相似：都用于将数据显示在界面中（没有Mustache灵活）   二、v-html 会将string的html解析出来并进行渲染   三、v-show 用法与v-if相似，也用于决定一个元素是否渲染  1、v-if和v-show对比 v-if：当条件为false时，包含v-if指令的元素，根本就不会存在于dom中 v-show：当条件为false时，v-sh">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-text.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-html.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-for.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.stop.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.prevent.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.keyCode.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.once.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-base.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-classObj.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-classArr.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-styleObj.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-styleArr.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88radio.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88checkbox.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88select1.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88select2.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-slot1.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-slot2.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-pre.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-cloak.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-once.png">
<meta property="article:published_time" content="2022-01-06T03:39:15.000Z">
<meta property="article:modified_time" content="2022-01-06T03:42:00.682Z">
<meta property="article:author" content="Han Qiu">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-text.png">
  
  
  <title>vue常用指令 - 寒秋の博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="vue常用指令">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-01-06 11:39" pubdate>
        2022年1月6日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.2k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      36 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">vue常用指令</h1>
            
            <div class="markdown-body">
              <h3 id="一、v-text"><a href="#一、v-text" class="headerlink" title="一、v-text"></a>一、v-text</h3><ul>
<li>与 Mustache比较相似：都用于将数据显示在界面中（没有Mustache灵活）</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-text.png" srcset="/img/loading.gif" lazyload alt="v-text"></p>
<h3 id="二、v-html"><a href="#二、v-html" class="headerlink" title="二、v-html"></a>二、v-html</h3><ul>
<li>会将string的html解析出来并进行渲染</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-html.png" srcset="/img/loading.gif" lazyload alt="v-html"></p>
<h3 id="三、v-show"><a href="#三、v-show" class="headerlink" title="三、v-show"></a>三、v-show</h3><ul>
<li>用法与v-if相似，也用于决定一个元素是否渲染</li>
</ul>
<h4 id="1、v-if和v-show对比"><a href="#1、v-if和v-show对比" class="headerlink" title="1、v-if和v-show对比"></a>1、v-if和v-show对比</h4><ul>
<li>v-if：当条件为false时，包含v-if指令的元素，根本就不会存在于dom中</li>
<li>v-show：当条件为false时，v-show只是给我们的元素添加一个行内样式 display：none</li>
</ul>
<h4 id="2、如何选择"><a href="#2、如何选择" class="headerlink" title="2、如何选择"></a>2、如何选择</h4><ul>
<li>需要在显示与隐藏之间频繁切换时，使用v-show</li>
<li>只有一次切换时，使用v-if（根据服务器传入数据决定是否渲染，大量使用）</li>
</ul>
<h3 id="四、v-if"><a href="#四、v-if" class="headerlink" title="四、v-if"></a>四、v-if</h3><ul>
<li>与js中if类似</li>
<li>Vue的条件指令可以根据表达式的值在DOM中渲染或销毁元素或组件</li>
<li>原理：v-if后面的条件为false时，对应的元素以及其子元素不会渲染，根本没有不会有对应的标签出现在DOM中</li>
</ul>
<h3 id="五、-v-else"><a href="#五、-v-else" class="headerlink" title="五、 v-else"></a>五、 v-else</h3><ul>
<li>与js中else类似</li>
<li>Vue的条件指令可以根据表达式的值在DOM中渲染或销毁元素或组件</li>
</ul>
<h3 id="六、v-else-if"><a href="#六、v-else-if" class="headerlink" title="六、v-else-if"></a>六、v-else-if</h3><ul>
<li>与js中else if类似</li>
<li>Vue的条件指令可以根据表达式的值在DOM中渲染或销毁元素或组件</li>
</ul>
<h3 id="七、v-for"><a href="#七、v-for" class="headerlink" title="七、v-for"></a>七、v-for</h3><h4 id="1、遍历数组"><a href="#1、遍历数组" class="headerlink" title="1、遍历数组"></a>1、遍历数组</h4><h5 id="1-1、不含索引"><a href="#1-1、不含索引" class="headerlink" title="1.1、不含索引"></a>1.1、不含索引</h5><ul>
<li><code>&lt;li v-for=&quot;item in names&quot;&gt;&#123;&#123; item &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<h5 id="1-2、含索引"><a href="#1-2、含索引" class="headerlink" title="1.2、含索引"></a>1.2、含索引</h5><ul>
<li><code>&lt;li v-for=&quot;(item, index) in names&quot;&gt;&#123;&#123; index+1 &#125;&#125;.&#123;&#123; item &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<h4 id="2、遍历对象"><a href="#2、遍历对象" class="headerlink" title="2、遍历对象"></a>2、遍历对象</h4><h5 id="2-1、获取value"><a href="#2-1、获取value" class="headerlink" title="2.1、获取value"></a>2.1、获取value</h5><ul>
<li><code>&lt;li v-for=&quot;item in info&quot;&gt;&#123;&#123; item &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<h5 id="2-2、获取value和key"><a href="#2-2、获取value和key" class="headerlink" title="2.2、获取value和key"></a>2.2、获取value和key</h5><ul>
<li><code>&lt;li v-for=&quot;(value, key) in info&quot;&gt;&#123;&#123; value &#125;&#125;-&#123;&#123; key &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<h5 id="2-3、获取value、key和index"><a href="#2-3、获取value、key和index" class="headerlink" title="2.3、获取value、key和index"></a>2.3、获取value、key和index</h5><ul>
<li><code>&lt;li v-for=&quot;(value, key, index) in info&quot;&gt;&#123;&#123; value &#125;&#125;-&#123;&#123; key &#125;&#125;-&#123;&#123; index &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<h4 id="3、v-for绑定key和不绑定key的区别"><a href="#3、v-for绑定key和不绑定key的区别" class="headerlink" title="3、v-for绑定key和不绑定key的区别"></a>3、v-for绑定key和不绑定key的区别</h4><ul>
<li>主要在于虚拟dom的复用，绑定key可以更好的复用</li>
<li>不推荐使用index作为key（在最后插入输入，重新渲染最后一条，使用index没有问题。但在中间插入时，后面数据的index都会发生改变，都会重新进行渲染，与不绑定key效果一样）</li>
<li>有相同父元素的子元素必须有独特的key，重复的key会造成渲染错误</li>
<li>官方示例：<code>&lt;li v-for=&quot;item in items&quot; :key=&quot;item.id&quot;&gt;&#123;&#123; item.text &#125;&#125;&lt;/li&gt;</code></li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-for.png" srcset="/img/loading.gif" lazyload alt="v-for"></p>
<h3 id="八、v-on"><a href="#八、v-on" class="headerlink" title="八、v-on"></a>八、v-on</h3><ul>
<li>绑定事件监听器</li>
<li>语法糖 @</li>
</ul>
<h4 id="1、v-on参数"><a href="#1、v-on参数" class="headerlink" title="1、v-on参数"></a>1、v-on参数</h4><h5 id="1-1、情况一（方法不需参数）"><a href="#1-1、情况一（方法不需参数）" class="headerlink" title="1.1、情况一（方法不需参数）"></a>1.1、情况一（方法不需参数）</h5><ul>
<li>调用方法时可以加()，也可以不加()</li>
</ul>
<h5 id="1-2、情况二（方法需要一个参数）"><a href="#1-2、情况二（方法需要一个参数）" class="headerlink" title="1.2、情况二（方法需要一个参数）"></a>1.2、情况二（方法需要一个参数）</h5><ul>
<li>调用方法时不加()：该形参被赋值为原生事件event</li>
<li>调用方法时加()：()中传入形参对应的实参。如果不传参数，形参会被赋值为undefined</li>
</ul>
<h5 id="1-3、情况三（方法需要其他参数和event）"><a href="#1-3、情况三（方法需要其他参数和event）" class="headerlink" title="1.3、情况三（方法需要其他参数和event）"></a>1.3、情况三（方法需要其他参数和event）</h5><ul>
<li>调用方法时不加()：第一个形参被赋值为event，其他参数为undefined</li>
<li>调用方法时加()：()中传入形参对应的实参。如果传入参数个数少于方法需要的参数个数（包含不传），其他形参会被赋值为undefined。可以通过$event传入event事件</li>
</ul>
<h4 id="2、修饰符"><a href="#2、修饰符" class="headerlink" title="2、修饰符"></a>2、修饰符</h4><h5 id="2-1、-stop"><a href="#2-1、-stop" class="headerlink" title="2.1、.stop"></a>2.1、.stop</h5><ul>
<li>调用event.stopPropagation()</li>
<li>停止冒泡</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.stop.png" srcset="/img/loading.gif" lazyload alt="v-on.stop"></p>
<h5 id="2-2、-prevent"><a href="#2-2、-prevent" class="headerlink" title="2.2、.prevent"></a>2.2、.prevent</h5><ul>
<li>调用event.prevenDefault</li>
<li>阻止默认行为（如点击按钮表单自动提交）</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.prevent.png" srcset="/img/loading.gif" lazyload alt="v-on.prevent"></p>
<h5 id="2-3、-keyCode-keyAlias"><a href="#2-3、-keyCode-keyAlias" class="headerlink" title="2.3、.{keyCode | keyAlias}"></a>2.3、.{keyCode | keyAlias}</h5><ul>
<li>只当事件是从特定键触发时才触发回调</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.keyCode.png" srcset="/img/loading.gif" lazyload alt="v-on.keyCode"></p>
<h5 id="2-4、-once"><a href="#2-4、-once" class="headerlink" title="2.4、.once"></a>2.4、.once</h5><ul>
<li>只触发一次回调</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-on.once.png" srcset="/img/loading.gif" lazyload alt="v-on.once"></p>
<h5 id="2-5、-native"><a href="#2-5、-native" class="headerlink" title="2.5、.native"></a>2.5、.native</h5><ul>
<li>监听组件根元素的原生事件，不限于click事件（例如：调用组件时，直接使用@click无法监听到点击事件）<ul>
<li><code>&lt;cpn @click=&quot;btnClick&quot;&gt;&lt;/cpn&gt; // 无法监听click</code></li>
<li><code>&lt;cpn @click.native=&quot;btnClick&quot;&gt;&lt;/cpn&gt; // 可以监听click</code></li>
</ul>
</li>
</ul>
<h3 id="九、v-bind"><a href="#九、v-bind" class="headerlink" title="九、v-bind"></a>九、v-bind</h3><ul>
<li>动态绑定数据</li>
<li>语法糖 :</li>
</ul>
<h4 id="1、绑定基本属性"><a href="#1、绑定基本属性" class="headerlink" title="1、绑定基本属性"></a>1、绑定基本属性</h4><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-base.png" srcset="/img/loading.gif" lazyload alt="v-bind-base"></p>
<h4 id="2、绑定class"><a href="#2、绑定class" class="headerlink" title="2、绑定class"></a>2、绑定class</h4><h5 id="2-1、对象语法"><a href="#2-1、对象语法" class="headerlink" title="2.1、对象语法"></a>2.1、对象语法</h5><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-classObj.png" srcset="/img/loading.gif" lazyload alt="v-bind-classObj"></p>
<h5 id="2-2、数组语法（了解）"><a href="#2-2、数组语法（了解）" class="headerlink" title="2.2、数组语法（了解）"></a>2.2、数组语法（了解）</h5><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-classArr.png" srcset="/img/loading.gif" lazyload alt="v-bind-classArr"></p>
<h4 id="3、绑定style"><a href="#3、绑定style" class="headerlink" title="3、绑定style"></a>3、绑定style</h4><h5 id="3-1、对象语法"><a href="#3-1、对象语法" class="headerlink" title="3.1、对象语法"></a>3.1、对象语法</h5><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-styleObj.png" srcset="/img/loading.gif" lazyload alt="v-bind-styleObj"></p>
<h5 id="3-2、数组语法（了解）"><a href="#3-2、数组语法（了解）" class="headerlink" title="3.2、数组语法（了解）"></a>3.2、数组语法（了解）</h5><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-bind-styleArr.png" srcset="/img/loading.gif" lazyload alt="v-bind-styleArr"></p>
<h3 id="十、v-model"><a href="#十、v-model" class="headerlink" title="十、v-model"></a>十、v-model</h3><h4 id="1、修饰符"><a href="#1、修饰符" class="headerlink" title="1、修饰符"></a>1、修饰符</h4><h5 id="1-1、lazy"><a href="#1-1、lazy" class="headerlink" title="1.1、lazy"></a>1.1、lazy</h5><ul>
<li>默认情况下，v-model默认是在input事件中同步输入框的数据的，也就是说，一旦有数据发生改变对应的data中的数据就会自动发生改变</li>
<li> lazy修饰符可以让数据在失去焦点或者回车时才会更新<br><code>&lt;input type=&quot;text&quot; v-model.lazy=&quot;message&quot;&gt;</code></li>
</ul>
<h5 id="1-2、number"><a href="#1-2、number" class="headerlink" title="1.2、number"></a>1.2、number</h5><ul>
<li>默认情况下，在输入框中无论我们输入的是字母还是数字，都会被当做字符串类型进行处理，但如果我们希望处理的是数字类型，那么最好直接将内容当做数字处理</li>
<li>number修饰符可以让输入框中输入的内容自动转成数字类型<br><code>&lt;input type=&quot;number&quot; v-model.number=&quot;age&quot;&gt;&lt;h2&gt;&#123;&#123;typeof age&#125;&#125;&lt;/h2&gt;</code></li>
</ul>
<h5 id="1-3、trim"><a href="#1-3、trim" class="headerlink" title="1.3、trim"></a>1.3、trim</h5><ul>
<li>如果输入的内容首尾有很多空格，通常我们希望将其去除，trim修饰符可以过滤内容左右两边的空格</li>
</ul>
<h4 id="2、v-model原理"><a href="#2、v-model原理" class="headerlink" title="2、v-model原理"></a>2、v-model原理</h4><figure class="highlight html"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></div></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;message&quot;</span>&gt;</span><br><span class="hljs-comment">&lt;!-- 等同于下面 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">v-bind:value</span>=<span class="hljs-string">&quot;message&quot;</span> <span class="hljs-attr">v-on:input</span>=<span class="hljs-string">&quot;message = $event.target.value&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>&#123;&#123; message &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="3、v-model结合radio类型"><a href="#3、v-model结合radio类型" class="headerlink" title="3、v-model结合radio类型"></a>3、v-model结合radio类型</h4><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88radio.png" srcset="/img/loading.gif" lazyload alt="v-model结合radio"></p>
<h4 id="4、v-model结合checkbox类型"><a href="#4、v-model结合checkbox类型" class="headerlink" title="4、v-model结合checkbox类型"></a>4、v-model结合checkbox类型</h4><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88checkbox.png" srcset="/img/loading.gif" lazyload alt="v-model结合checkbox"></p>
<h4 id="5、v-model结合select类型"><a href="#5、v-model结合select类型" class="headerlink" title="5、v-model结合select类型"></a>5、v-model结合select类型</h4><p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88select1.png" srcset="/img/loading.gif" lazyload alt="v-model结合select1"><br><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-model%E7%BB%93%E5%90%88select2.png" srcset="/img/loading.gif" lazyload alt="v-model结合select2"></p>
<h3 id="十一、v-slot"><a href="#十一、v-slot" class="headerlink" title="十一、v-slot"></a>十一、v-slot</h3><h4 id="1、插槽类型"><a href="#1、插槽类型" class="headerlink" title="1、插槽类型"></a>1、插槽类型</h4><h5 id="1-1、匿名插槽（隐含name-“default”）"><a href="#1-1、匿名插槽（隐含name-“default”）" class="headerlink" title="1.1、匿名插槽（隐含name=“default”）"></a>1.1、匿名插槽（隐含name=“default”）</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span>匿名插槽默认内容<span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-2、具名插槽"><a href="#1-2、具名插槽" class="headerlink" title="1.2、具名插槽"></a>1.2、具名插槽</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;top&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;center&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;bottom&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-3、作用域插槽（父组件替换插槽的标签，但是内容由子组件来提供）"><a href="#1-3、作用域插槽（父组件替换插槽的标签，但是内容由子组件来提供）" class="headerlink" title="1.3、作用域插槽（父组件替换插槽的标签，但是内容由子组件来提供）"></a>1.3、作用域插槽（父组件替换插槽的标签，但是内容由子组件来提供）</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 子组件中定义插槽，name为top（插槽名），data（属性名自己取）为items（子组件中的数据） --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;top&quot;</span> <span class="hljs-attr">:data</span>=<span class="hljs-string">&quot;items&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 父组件调用子组件，子组件名以cpn为例，slotProps（名称自定义） .data（调用子组件中绑定的data自定义属性）--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">cpn</span> <span class="hljs-attr">slot</span>=<span class="hljs-string">&quot;top&quot;</span> <span class="hljs-attr">slot-scope</span>=<span class="hljs-string">&quot;slotProps&quot;</span>&gt;</span>&#123;&#123; slotProps.data &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">cpn</span>&gt;</span><br></code></pre></td></tr></table></figure>

<ul>
<li>作用域插槽示例：子组件中包括一组数据，比如：pLanguages: [‘JavaScript’, ‘Python’, ‘Swift’, ‘Go’, ‘C++’]，需要在多个界面进行展示（某些界面是以水平方向一一展示的，某些界面是以列表形式展示的）</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-slot1.png" srcset="/img/loading.gif" lazyload alt="v-slot1"><br><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-slot2.png" srcset="/img/loading.gif" lazyload alt="v-slot2"></p>
<h4 id="2、v-slot"><a href="#2、v-slot" class="headerlink" title="2、v-slot"></a>2、v-slot</h4><ul>
<li>在vue2.6及已上版本，slot 和slot-scope已经开始废弃， 有了新的替代: v-slot，v-slot只能用在template 上，和组件标签上</li>
<li>语法糖 #</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 子组件中定义插槽方式不变 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;top&quot;</span> <span class="hljs-attr">:data</span>=<span class="hljs-string">&quot;items&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br><span class="hljs-comment">&lt;!-- 父组件调用子组件，使用v-slot代替slot和slot-scope --&gt;</span><br><span class="hljs-comment">&lt;!-- #top为v-slot:top的简写 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">cpn</span> #<span class="hljs-attr">top</span>=<span class="hljs-string">&quot;slotProps&quot;</span>&gt;</span>&#123;&#123; slotProps.data &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">cpn</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="十二、v-pre（不需要表达式）"><a href="#十二、v-pre（不需要表达式）" class="headerlink" title="十二、v-pre（不需要表达式）"></a>十二、v-pre（不需要表达式）</h3><ul>
<li>v-pre用于跳过这个元素和它子元素的编译过程，用于显示原本的Mustache语法</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-pre.png" srcset="/img/loading.gif" lazyload alt="v-pre"></p>
<h3 id="十三、-v-cloak（不需要表达式）"><a href="#十三、-v-cloak（不需要表达式）" class="headerlink" title="十三、 v-cloak（不需要表达式）"></a>十三、 v-cloak（不需要表达式）</h3><ul>
<li>这个指令保持在元素上直到关联实例结束编译（vue解析完成后，元素上的v-cloak属性消失）</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-cloak.png" srcset="/img/loading.gif" lazyload alt="v-cloak"></p>
<h3 id="十四、v-once（不需要表达式）"><a href="#十四、v-once（不需要表达式）" class="headerlink" title="十四、v-once（不需要表达式）"></a>十四、v-once（不需要表达式）</h3><ul>
<li>元素和组件只渲染一次，不会随着数据的改变而改变</li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/vue/vueDirectives+computed/v-once.png" srcset="/img/loading.gif" lazyload alt="v-once"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/vue/">vue</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/vue/">vue</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/06/vue%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7computed%E5%92%8C%E4%BE%A6%E5%90%AC%E5%99%A8watch/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">vue计算属性computed和侦听器watch</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/06/linux%E4%B8%AD%E5%AE%89%E8%A3%85nginx/">
                        <span class="hidden-mobile">linux中安装nginx</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
